var setting = {
    async: {
        enable: true,
        url: "../../Cloud/Tree/TreeAjaxHelper.ashx?method=GetTreeJsonData&treeTypeCode=" + $("#hidTreeTypeCode").val() + "&isFromCloud=" + $("#hidIsFromCloud").val() + "&questionReferenceValue=" + $("#hidQuestionReferenceValue").val() + "&isShowTreeCount=" + $("#hidIsShowTreeCount").val(),
        autoParam: ["id", "name=n", "level=lv"],
        otherParam: { "otherParam": "zTreeAsyncTest" },
        dataFilter: filter
    },
    check: {
        enable: $("#hidIsMutiSelect").val() == "Y" ? true : false
    },
    view: {
        expandSpeed: "",
        addHoverDom: addHoverDom,
        removeHoverDom: removeHoverDom,
        selectedMulti: false,
        nameIsHTML: true
    },
    edit: {
        enable: $("#hidIsFromCloud").val()!="Y"?true:false
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        beforeClick: beforeClick,
        beforeRemove: beforeRemove,
        beforeRename: beforeRename,
        onRename: onRename,
        beforeDrag: beforeDrag,
        beforeDrop: beforeDrop,
        onDrop: onDrop,
        onClick: onClick,
        onRemove: onRemove,
        onCheck: onCheck,
        beforeEditName: onBeforeEditName,
        onAsyncSuccess: function (event, treeId, treeNode, msg) { $("#treeLoading").hide();}
    }
};

function onBeforeEditName(treeId, treeNode)
{
    treeNode.name = treeNode.name1;
    treeNode.name1 = treeNode.name1;
}

function beforeClick(treeId, treeNode) { }
//多选
function onCheck(event, treeId, treeNode, clickFlag) {
    var zTree = $.fn.zTree.getZTreeObj("divTreeList");
    var nodes = zTree.getCheckedNodes(true);
    selectTree_SelectedIDs = "";
    selectTree_SelectedNames = "";
    selectTree_SelectedFullNames = "";
    for (var i = 0; i < nodes.length; i++) {
        selectTree_SelectedIDs += nodes[i].id + ",";
        selectTree_SelectedNames += nodes[i].name1 + ",";
        selectTree_SelectedFullNames += nodes[i].fullTreeName + ",";
    }
    if (selectTree_SelectedIDs.length > 0) selectTree_SelectedIDs = selectTree_SelectedIDs.substring(0, selectTree_SelectedIDs.length - 1);
    if (selectTree_SelectedNames.length > 0) selectTree_SelectedNames = selectTree_SelectedNames.substring(0, selectTree_SelectedNames.length - 1);
    if (selectTree_SelectedFullNames.length > 0) selectTree_SelectedFullNames = selectTree_SelectedFullNames.substring(0, selectTree_SelectedFullNames.length - 1);
    if (isAddNode || isRemoveNode) { isAddNode = false; isRemoveNode = false; return; }//防止新增或删除操作时，刷新界面，造成体验差
    if (typeof (doSelectTreeCallback) == 'function') {
        doSelectTreeCallback(selectTree_SelectedIDs, selectTree_SelectedNames, selectTree_SelectedFullNames); //回调函数,在aspx页面时实现其方法
        //$("#divTreePanel").fadeOut();
    }
    else {
        Dialog.error("选择组织分类失败,没有实现回调方法：doSelectTreeCallback");
    }
}
function onClick(event, treeId, treeNode) {
    if ($("#hidIsMutiSelect").val() == "Y") { return; }
    var zTree = $.fn.zTree.getZTreeObj("divTreeList"),

    nodes = zTree.getSelectedNodes(),
    selectTree_SelectedIDs = "";
    selectTree_SelectedNames = "";
    selectTree_SelectedFullNames = "";
    nodes.sort(function compare(a, b) { return a.id - b.id; });
    for (var i = 0, l = nodes.length; i < l; i++) {
        selectTree_SelectedNames += nodes[i].name1 + ",";
        selectTree_SelectedFullNames += nodes[i].fullTreeName + ",";
        selectTree_SelectedIDs += nodes[i].id + ",";
    }
    if (selectTree_SelectedNames.length > 0) selectTree_SelectedNames = selectTree_SelectedNames.substring(0, selectTree_SelectedNames.length - 1);
    if (selectTree_SelectedFullNames.length > 0) selectTree_SelectedFullNames = selectTree_SelectedFullNames.substring(0, selectTree_SelectedFullNames.length - 1);
    if (selectTree_SelectedIDs.length > 0) selectTree_SelectedIDs = selectTree_SelectedIDs.substring(0, selectTree_SelectedIDs.length - 1);
    if (isAddNode || isRemoveNode) { isAddNode = false; isRemoveNode = false; return; }//防止新增或删除操作时，刷新界面，造成体验差
    if (typeof (doSelectTreeCallback) == 'function') {
        doSelectTreeCallback(selectTree_SelectedIDs, selectTree_SelectedNames, selectTree_SelectedFullNames); //回调函数,在aspx页面时实现其方法
        //$("#divTreePanel").fadeOut();
    }
    else {
        Dialog.error("未实现回调方法：doSelectTreeCallback");
    }
}

function filter(treeId, parentNode, childNodes) {
    if (!childNodes) return null;
    for (var i = 0, l = childNodes.length; i < l; i++) {
        childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
    }
    return childNodes;
}
var log, className = "dark";
var isRemoveNode = false;
function beforeRemove(treeId, treeNode) {
    isRemoveNode = true;
    ajaxTodo("../../Cloud/Tree/TreeAjaxHelper.ashx?method=DeleteTree&treeType=" + $("#hidTreeTypeCode").val() + "&treeID=" + treeNode.id, function (json) {
        if (json.flag == "Y") {
            //alertMsg.correct("删除成功");
            var treeObj = $.fn.zTree.getZTreeObj("divTreeList");
            treeObj.removeNode(treeNode);
            //删除成功后，回调清空方法
            if (typeof (doDeleteTreeCallback) == 'function') {
                doDeleteTreeCallback(treeNode.id); //回调函数,在aspx页面时实现其方法
                //$("#divTreePanel").fadeOut();
            }
        }
        else {
            Dialog.error(json.errorMsg);
            return false;
        }
    });
    return false;
}

//删除组织
function onRemove(e, treeId, treeNode) {
}

function beforeRename(treeId, treeNode, newName) {
    if (newName.length == 0) {
        alert("节点名称不能为空.");
        var zTree = $.fn.zTree.getZTreeObj("divTreeList");
        setTimeout(function () { zTree.editName(treeNode) }, 10);
        return false;
    }
    treeNode.name1 = newName;
    var fullName = newName;
    if (treeNode.fullTreeName.lastIndexOf("/") > -1) {
        fullName = treeNode.fullTreeName.substring(0, treeNode.fullTreeName.lastIndexOf("/")) + "/" + newName;
    }
    treeNode.fullTreeName = fullName;
    return true;
}
//修改名称
function onRename(e, treeId, treeNode) {
    ajaxTodo("../../Cloud/Tree/TreeAjaxHelper.ashx?method=SaveSimpleData&treeID=" + treeNode.id + "&treeName=" + escape(treeNode.name), function (json) {
        if (json.flag == "Y") {
            //alertMsg.correct("修改成功");
        }
        else {
            Dialog.error(json.msg);
        }
    });
}
var newCount = 1;
var isAddNode = false;
function addHoverDom(treeId, treeNode) {
    if ($("#hidIsFromCloud").val() == "Y") return;
    var sObj = $("#" + treeNode.tId + "_span");
    if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0) return;
    var addStr = "<button type='button' class='add' id='addBtn_" + treeNode.id + "' title='新增' onfocus='this.blur();'></button>";
    sObj.after(addStr);
    var btn = $("#addBtn_" + treeNode.id);
    if (btn) btn.bind("click", function () {
        isAddNode = true;
        var zTree = $.fn.zTree.getZTreeObj("divTreeList");
        var treeID = "";
        var count = newCount++;
        //新增子组织结点
        ajaxTodo("../../Cloud/Tree/TreeAjaxHelper.ashx?method=SaveSimpleData&treeTypeCode=" + $("#hidTreeTypeCode").val() + "&parentTreeID=" + treeNode.id + "&treeName=" + escape("新增子目录" + count), function (json) {
            if (json.flag == "Y") {
                //alertMsg.correct("新增成功");
                treeID = json.treeID;
                zTree.addNodes(treeNode, { id: treeID, pId: treeNode.id, name: json.treeName, name1: json.treeName, fullTreeName: json.fullTreeName });
                var newNodes = zTree.getNodeByParam("id", treeID);
            }
            else {
                Dialog.error(json.msg);
            }
        });
    });
};
function removeHoverDom(treeId, treeNode) {
    $("#addBtn_" + treeNode.id).unbind().remove();
};
function beforeDrag(treeId, treeNodes) {
    for (var i = 0, l = treeNodes.length; i < l; i++) {
        if (treeNodes[i].drag === false) {
            return false;
        }
    }
    return true;
}
function beforeDrop(treeId, treeNodes, targetNode, moveType) {
    return targetNode ? targetNode.drop !== false : true;
}
function onDrop(event, treeId, treeNodes, targetNode, moveType) {
    for (var i = 0, l = treeNodes.length; i < l; i++) {
        if (treeNodes[i].drag === false) {
            return false;
        }
        else {
            alert(moveType); return;
            ajaxTodo("../../Cloud/Tree/TreeAjaxHelper.ashx?method=DropTreeNode&dropTreeID=" + treeNodes[i].id + "&targetTreeID=" + targetNode.id + "&dropType=" + moveType, function (json) {
                if (json.flag == "Y") {
                    //alertMsg.correct("拖动成功");
                }
                else {
                    Dialog.error("数据错误");
                }
            });
        }
    }
}
$(function () {
    if ($("#hidAutoLoadTree").val() == "Y") {//打开页面就加载模式，另外一种是showMenu事件触发
        var objSelectTree = $.fn.zTree.init($("#divTreeList"), setting);
        objSelectTree.setting.edit.renameTitle = "修改";
        objSelectTree.setting.edit.removeTitle = "删除";
        if ($("#hidIsMutiSelect").val() == "Y") {
            objSelectTree.setting.check.chkboxType = { "Y": "", "N": "" };
        }
    }
});
var _questionTypeCode = "";
var _treetextboxID = "";
function showMenu(id) {
    $("#treeLoading").show();
    if (id == "" || id == undefined || id == null) return;
    setting.async.url = "../../Cloud/Tree/TreeAjaxHelper.ashx?method=GetTreeJsonData&treeTypeCode=" + $("#hidTreeTypeCode").val() + "&isFromCloud=" + $("#hidIsFromCloud").val() + "&questionReferenceValue=" + $("#hidQuestionReferenceValue").val() + "&isShowTreeCount=" + $("#hidIsShowTreeCount").val();
    var objSelectTree = $.fn.zTree.init($("#divTreeList"), setting);
    objSelectTree.setting.edit.renameTitle = "修改";
    objSelectTree.setting.edit.removeTitle = "删除";
    if ($("#hidIsMutiSelect").val() == "Y") {
        objSelectTree.setting.check.chkboxType = { "Y": "", "N": "" };
    }
    var cityObj = $("#"+id);
    var cityOffset = $("#"+id).offset();
    $("#divTreePanel").css({ left: cityOffset.left + "px", top: cityOffset.top + cityObj.outerHeight() + "px" }).slideDown("fast");
    cityObj.attr("disabled", "true");
    _treetextboxID = id;
    $("body").bind("mousedown", onBodyDown);

}
function onBodyDown(event) {
    if (!(event.target.id == "btnSelectTree" || event.target.id == "divTreeList" || event.target.id == "divTreePanel" || $(event.target).parents("#divTreePanel").length > 0)) {
        hideMenu();
        $("#" + _treetextboxID).removeAttr("disabled");
    }
}function hideMenu() {
    $("#divTreePanel").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);
}